﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;
using BranchOfSansar.Sansar.Enquiry;

namespace BranchOfSansar.Register.Authentication
{
    public partial class frmRole : Form
    {
        public frmRole()
        {
            InitializeComponent();
        }
        private void BindRole()
        {
            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                lstRoles.DataSource = reg.RoleList().Select(s => new ComboItem() { Display="("+s.RoleId+") "+s.Name, IntValue=s.RoleId }).ToArray();
            }
           
        }

        private void BindPermission()
        {
 
        }

        private void btnAddRole_Click(object sender, EventArgs e)
        {
            frmRegRole frm = new frmRegRole();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindRole();
            }
        }

        private void frmRole_Load(object sender, EventArgs e)
        {           
            BindRole();
            
        }

        private void lstRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboItem item = (ComboItem)lstRoles.SelectedItem;
           
            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                var list=reg.RoleAndPermissionList(item.IntValue);                
                lstPermission.DataSource =list;
            }
            lstPermission.ExpandAll();            
        }

        private void lstPermission_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {           
            
            ComboItem item = (ComboItem)lstRoles.SelectedItem;
            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                reg.SetRolePermission(e.Node.Checked,item.IntValue,int.Parse(e.Node.GetDisplayText("PerId")));             
            }
        }

        
    }
}
